package com.hsy.linemoney.modules.my.myteam;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的团队 - 将会用网页替代
 * Created by Administrator on 2016/7/1.
 */
public class MyTeamActivity extends CommonBaseActivityWithSimpleActionBar {
    private WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_team);
        setTitle(R.string.title_my_team);
        CommonUtils.showProgressDialog(this);
        webView = (WebView) findViewById(R.id.web_view);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress >= 100) {
                    CommonUtils.hideProgressDialog();
//                    showToast("请在此处登录一次");
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                DPLogUtil.d("url=" + url);
                return true;
            }
        });
//        settings.setAppCacheEnabled(true);
        webView.loadUrl(LineMoneyContext.getServer() + "web/guest/my-clients");
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == event.getKeyCode() && KeyEvent.ACTION_UP == event.getAction() && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

//    public String sendPost(String url, String username, String password) {
//        // 根据url获得HttpPost对象
//        HttpPost httpRequest = new HttpPost(url);
//        // 取得默认的HttpClient
//        DefaultHttpClient httpclient = new DefaultHttpClient();
//        String strResult = null;
//        // NameValuePair实现请求参数的封装
//        List<NameValuePair> params = new ArrayList<NameValuePair>();
//        params.add(new BasicNameValuePair("_tk", "codefrom"));
//        params.add(new BasicNameValuePair("name", username));
//        params.add(new BasicNameValuePair("pass", password));
//        httpRequest.addHeader("Accept", "application/json, text/javascript, */*; q=0.01");
//        httpRequest.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        httpRequest.addHeader("Origin", "http://www.codefrom.com");
//        httpRequest.addHeader("Referer", "http://www.codefrom.com/login");
//        httpRequest.addHeader("X-Requested-With", "XMLHttpRequest");
//        try {
//            // 添加请求参数到请求对象
//            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//            // 获得响应对象
//            HttpResponse httpResponse = httpclient.execute(httpRequest);
//            // 判断是否请求成功
//            if (httpResponse.getStatusLine().getStatusCode() == 200) {
//                // 获得响应返回Json格式数据
//                strResult = EntityUtils.toString(httpResponse.getEntity());
//                return strResult;
//            } else {
//                strResult = "错误响应:" + httpResponse.getStatusLine().toString();
//            }
//        } catch (ClientProtocolException e) {
//            strResult = "错误响应:" + e.getMessage().toString();
//            e.printStackTrace();
//            return strResult;
//        } catch (IOException e) {
//            strResult = "错误响应:" + e.getMessage().toString();
//            e.printStackTrace();
//            return strResult;
//        } catch (Exception e) {
//            strResult = "错误响应:" + e.getMessage().toString();
//            e.printStackTrace();
//            return strResult;
//        }
//        return strResult;
//    }

}
